package com.icehelper.hades.base.controller;


import com.github.pagehelper.Page;
import com.icehelper.hades.base.exception.CommonErrorCode;
import java.util.Collection;
import java.util.List;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

/**
 * 分页结果的返回类
 * @param <DATA> 具体的数据类型
 * @see Pagination
 */
@Data
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class BizPageResponse<DATA> extends BizResponse<Collection<DATA>> {

  private int pageNo;
  private int pageSize;
  private long totalPageCount;
  private long totalItemCount;

  public static <T> BizPageResponse<T> ofPage(Pagination<T> page) {
    BizPageResponse<T> pr = new BizPageResponse<>();
    pr.setCode(CommonErrorCode.SUCCESS.code());
    pr.setMessage(CommonErrorCode.SUCCESS.msg());
    pr.setTotalItemCount(page.getTotalItemCount());
    pr.setTotalPageCount(page.getTotalPageCount());
    pr.setPageNo(page.getPageNo());
    pr.setPageSize(page.getPageSize());
    pr.setData(page.getData());
    return pr;
  }

  public static <T> BizPageResponse<T> ofPage(Page page, List<T> list) {
    Pagination pagination = toPagination(page,list);
    return ofPage(pagination);
  }

  public static <T> Pagination<T> toPagination(Page page,List<T> list) {
    Pagination<T> pagination = new Pagination<T>();
    pagination.setTotalPageCount(page.getPages());
    pagination.setPageNo(page.getPageNum());
    pagination.setPageSize(page.getPageSize());
    pagination.setTotalItemCount(page.getTotal());
    pagination.setData(list);
    return pagination;
  }


}